<?php

namespace App\Http\Controllers\API;

use App\Http\Resources\ExhibitorInformationResource;
use App\Models\ExhibitorInformation;
use App\Models\JournalContent;
use App\Models\MessageProcessCount;
use App\Models\SafetyAgreement;
use App\Services\AliSmsService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class SafetyAgreementController extends Controller
{
    public function add(Request $request)
    {
        $exhibitor_information_id = session()->get('exhibitor_information_id');
        $exhibitor_message_id = session()->get('exhibitor_message_id');

        if (!$exhibitor_message_id) {
            return $this->failure('数据错误，重新登录');
        }

        if (!$exhibitor_information_id) {
            return $this->failure('登录超时，重新登录');
        }

        $message_process_count = MessageProcessCount::query()
            ->where('model_type_id', 1)
            ->where('certificates_exhibitor_information_id', $exhibitor_information_id)
            ->first();

        if ($message_process_count) {
            return $this->failure('已完成');
        }

        $param = $request->all();

        $file_url = $param['file'];

        if (!$file_url) {
            return $this->failure('请上传安全责任书');
        }

        $data['files'] = $file_url;
        $data['operator_id'] = $exhibitor_information_id;
        $safety_agreement = SafetyAgreement::query()->create($data);

        MessageProcessCount::query()->insert(
            [
                'model_type_id' => 1,
                'model_id' => $safety_agreement->id,
                'certificates_exhibitor_information_id' => $exhibitor_information_id,
                'exhibitor_message_id' => $exhibitor_message_id
            ]
        );

        if ($safety_agreement) {
            return $this->success('成功');
        } else {
            return $this->failure('失败');
        }
    }

    public function show()
    {
        $exhibitor_information_id = session()->get('exhibitor_information_id');
        $exhibitor_message_id = session()->get('exhibitor_message_id');

        if (!$exhibitor_message_id) {
            return $this->failure('数据错误，重新登录');
        }

        if (!$exhibitor_information_id) {
            return $this->failure('登录超时，重新登录');
        }

        $message_process_count = MessageProcessCount::query()
            ->where('model_type_id', 1)
            ->where('certificates_exhibitor_information_id', $exhibitor_information_id)
            ->first();

        if ($message_process_count) {
            $data = SafetyAgreement::where('operator_id', $exhibitor_information_id)->first();
        }

        return $this->success($data ?? '');
    }

}
